﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="../Resource/Css/site.css" rel="stylesheet" />
    <script src="../Resource/Jquery/jquery-1.10.2.js"></script>
</head>
<body>
    <div id="container"></div>
</body>
</html>
<script>
    var data = function () {
        return [
       {
           "CUSTID": "1000000495",
           "IBSNO": "000154",
           "NAME": "王丽平",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "wlp@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中国中信集团有限公司"
       },
       {
           "CUSTID": "1000000495",
           "IBSNO": "002645",
           "NAME": "刘靖",
           "RELATIONTYPE": "ZBUR14",
           "RELATETYPEALIAS": "客户经理(资产管理业务部)",
           "DEPNM": "资产管理业务",
           "EMAIL": "ljzg@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中国中信集团有限公司"
       },
       {
           "CUSTID": "1000000495",
           "IBSNO": "008757",
           "NAME": "解放",
           "RELATIONTYPE": "ZBUR30",
           "RELATETYPEALIAS": "风控经理(风险管理部)",
           "DEPNM": "风险控制部",
           "EMAIL": "xiefang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中国中信集团有限公司"
       },
       {
           "CUSTID": "1000000610",
           "IBSNO": "003103",
           "NAME": "王姝文",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "wshuwen@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信国安集团公司"
       },
       {
           "CUSTID": "1000000610",
           "IBSNO": "008757",
           "NAME": "解放",
           "RELATIONTYPE": "ZBUR30",
           "RELATETYPEALIAS": "风控经理(风险管理部)",
           "DEPNM": "风险控制部",
           "EMAIL": "xiefang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中信国安集团公司"
       },
       {
           "CUSTID": "1000004374",
           "IBSNO": "000168",
           "NAME": "邱志千",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "qzq@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信戴卡股份有限公司"
       },
       {
           "CUSTID": "1000000021",
           "IBSNO": "002911",
           "NAME": "黄威",
           "RELATIONTYPE": "ZBUR08",
           "RELATETYPEALIAS": "客户经理(固定收益部)",
           "DEPNM": "固定收益部",
           "EMAIL": "weihuang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信银行股份有限公司"
       },
       {
           "CUSTID": "1000000021",
           "IBSNO": "000181",
           "NAME": "杨有燕",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "yyy@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中信银行股份有限公司"
       },
       {
           "CUSTID": "1000000021",
           "IBSNO": "012966",
           "NAME": "龚俊",
           "RELATIONTYPE": "ZBUR14",
           "RELATETYPEALIAS": "客户经理(资产管理业务部)",
           "DEPNM": "资产管理业务",
           "EMAIL": "gongjun@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信银行股份有限公司"
       },
       {
           "CUSTID": "1000000021",
           "IBSNO": "008757",
           "NAME": "解放",
           "RELATIONTYPE": "ZBUR30",
           "RELATETYPEALIAS": "风控经理(风险管理部)",
           "DEPNM": "风险控制部",
           "EMAIL": "xiefang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中信银行股份有限公司"
       },
       {
           "CUSTID": "1000006265",
           "IBSNO": "000217",
           "NAME": "陈杨",
           "RELATIONTYPE": "ZBUR08",
           "RELATETYPEALIAS": "客户经理(固定收益部)",
           "DEPNM": "固定收益部",
           "EMAIL": "cy@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信财务有限公司"
       },
       {
           "CUSTID": "1000000061",
           "IBSNO": "015661",
           "NAME": "曹上",
           "RELATIONTYPE": "ZBUR08",
           "RELATETYPEALIAS": "客户经理(固定收益部)",
           "DEPNM": "固定收益部",
           "EMAIL": "caoshang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "信诚人寿保险有限公司"
       },
       {
           "CUSTID": "1000000061",
           "IBSNO": "003157",
           "NAME": "吕伟育",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "lwy@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "信诚人寿保险有限公司"
       },
       {
           "CUSTID": "1000000061",
           "IBSNO": "004181",
           "NAME": "刘彬",
           "RELATIONTYPE": "ZBUR10",
           "RELATETYPEALIAS": "客户经理(股票销售交易部)",
           "DEPNM": "股票销售交易部",
           "EMAIL": "liub@citics.com",
           "FIXTEL": "+861060838994",
           "MOBILE": "+8618618493111",
           "ISMAINIBS": "1",
           "CUSTNM": "信诚人寿保险有限公司"
       },
       {
           "CUSTID": "1000003592",
           "IBSNO": "011744",
           "NAME": "于小荻",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "",
           "EMAIL": "yuxd@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信信托有限责任公司"
       },
       {
           "CUSTID": "1000003592",
           "IBSNO": "000227",
           "NAME": "窦长宏",
           "RELATIONTYPE": "ZBUR08",
           "RELATETYPEALIAS": "客户经理(固定收益部)",
           "DEPNM": "固定收益部",
           "EMAIL": "changhong@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信信托有限责任公司"
       },
       {
           "CUSTID": "1000003592",
           "IBSNO": "000056",
           "NAME": "温平丽",
           "RELATIONTYPE": "ZBUR10",
           "RELATETYPEALIAS": "客户经理(股票销售交易部)",
           "DEPNM": "股票销售交易部",
           "EMAIL": "wpl@citics.com",
           "FIXTEL": "+861060838983",
           "MOBILE": "+8613910617192",
           "ISMAINIBS": "1",
           "CUSTNM": "中信信托有限责任公司"
       },
       {
           "CUSTID": "1000003592",
           "IBSNO": "000381",
           "NAME": "关微",
           "RELATIONTYPE": "ZBUR12",
           "RELATETYPEALIAS": "客户经理(证券金融业务线)",
           "DEPNM": "证券金融业务线",
           "EMAIL": "guanwei@citics.com",
           "FIXTEL": "+861060838539",
           "MOBILE": "+8618611701216",
           "ISMAINIBS": "1",
           "CUSTNM": "中信信托有限责任公司"
       },
       {
           "CUSTID": "1000000349",
           "IBSNO": "002715",
           "NAME": "李黎",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "lilibj@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中信重工机械股份有限公司"
       },
       {
           "CUSTID": "1000000349",
           "IBSNO": "003206",
           "NAME": "朱烨辛",
           "RELATIONTYPE": "ZBUR03",
           "RELATETYPEALIAS": "行业专家关系",
           "DEPNM": "投资银行委员会",
           "EMAIL": "zhuyx@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中信重工机械股份有限公司"
       },
       {
           "CUSTID": "1000000349",
           "IBSNO": "008757",
           "NAME": "解放",
           "RELATIONTYPE": "ZBUR30",
           "RELATETYPEALIAS": "风控经理(风险管理部)",
           "DEPNM": "风险控制部",
           "EMAIL": "xiefang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "中信重工机械股份有限公司"
       },
       {
           "CUSTID": "1000000511",
           "IBSNO": "000138",
           "NAME": "蒋昱辰",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "jyc@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信房地产股份有限公司"
       },
       {
           "CUSTID": "1000002686",
           "IBSNO": "002911",
           "NAME": "黄威",
           "RELATIONTYPE": "ZBUR08",
           "RELATETYPEALIAS": "客户经理(固定收益部)",
           "DEPNM": "固定收益部",
           "EMAIL": "weihuang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "华夏基金管理有限公司"
       },
       {
           "CUSTID": "1000002686",
           "IBSNO": "002959",
           "NAME": "郭谷城",
           "RELATIONTYPE": "ZBUR10",
           "RELATETYPEALIAS": "客户经理(股票销售交易部)",
           "DEPNM": "股票销售交易部",
           "EMAIL": "ggc@citics.com",
           "FIXTEL": "+861060838835",
           "MOBILE": "+8613910898581",
           "ISMAINIBS": "1",
           "CUSTNM": "华夏基金管理有限公司"
       },
       {
           "CUSTID": "1000002686",
           "IBSNO": "008757",
           "NAME": "解放",
           "RELATIONTYPE": "ZBUR30",
           "RELATETYPEALIAS": "风控经理(风险管理部)",
           "DEPNM": "风险控制部",
           "EMAIL": "xiefang@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "0",
           "CUSTNM": "华夏基金管理有限公司"
       },
       {
           "CUSTID": "1000003215",
           "IBSNO": "003440",
           "NAME": "赵潇潇",
           "RELATIONTYPE": "ZBUR08",
           "RELATETYPEALIAS": "客户经理(固定收益部)",
           "DEPNM": "",
           "EMAIL": "zhxx@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信产业投资基金管理有限公司"
       },
       {
           "CUSTID": "1000003215",
           "IBSNO": "003559",
           "NAME": "刘炜敏",
           "RELATIONTYPE": "ZBUR01",
           "RELATETYPEALIAS": "投行IBS",
           "DEPNM": "投资银行委员会",
           "EMAIL": "lwm@citics.com",
           "FIXTEL": "",
           "MOBILE": "",
           "ISMAINIBS": "1",
           "CUSTNM": "中信产业投资基金管理有限公司"
       },
       //{
       //    "CUSTID": "",
       //    "IBSNO": "",
       //    "NAME": "",
       //    "RELATIONTYPE": "",
       //    "RELATETYPEALIAS": "",
       //    "DEPNM": "",
       //    "EMAIL": "",
       //    "FIXTEL": "",
       //    "MOBILE": "",
       //    "ISMAINIBS": "",
       //    "CUSTNM": ""
       //},
       {
           "CUSTID": "1000002686",
           "CUSTNM": "华夏基金管理有限公司",
           "IBSNO": "002959",
           "NAME": "郭谷城",
           "RELATETYPE": "2",
           "RELATETYPEALIAS": "客户经理助理",
           "DEPNM": "股票销售部",
           "EMAIL": "ggc@citics.com",
           "FIXTEL": "60838835",
           "MOBILE": "13910898581",
           "ISMAINIBS": "0"
       },
       {
           "CUSTID": "1000002686",
           "CUSTNM": "华夏基金管理有限公司",
           "IBSNO": "016957",
           "NAME": "赵征",
           "RELATETYPE": "2",
           "RELATETYPEALIAS": "客户经理助理",
           "DEPNM": "股票销售部",
           "EMAIL": "zhaozheng@citics.com",
           "FIXTEL": "60836241",
           "MOBILE": "18611249980",
           "ISMAINIBS": "0"
       },
       {
           "CUSTID": "1000003592",
           "CUSTNM": "中信信托有限责任公司",
           "IBSNO": "000056",
           "NAME": "温平丽",
           "RELATETYPE": "1",
           "RELATETYPEALIAS": "客户经理",
           "DEPNM": "股票销售部",
           "EMAIL": "wpl@citics.com",
           "FIXTEL": "60838983",
           "MOBILE": "13910617192",
           "ISMAINIBS": "1"
       },
       {
           "CUSTID": "1000003592",
           "CUSTNM": "中信信托有限责任公司",
           "IBSNO": "012340",
           "NAME": "廉雪飞",
           "RELATETYPE": "2",
           "RELATETYPEALIAS": "客户经理助理",
           "DEPNM": "股票销售部",
           "EMAIL": "lianxf@citics.com",
           "FIXTEL": "60836233",
           "MOBILE": "18911861230",
           "ISMAINIBS": "0"
       },
       {
           "CUSTID": "1000007033",
           "CUSTNM": "CITICSInt'lInvestmentManagemenet",
           "IBSNO": "016503",
           "NAME": "冯春",
           "RELATETYPE": "1",
           "RELATETYPEALIAS": "客户经理",
           "DEPNM": "股票销售部",
           "EMAIL": "fengchun@citics.com",
           "FIXTEL": "20262286",
           "MOBILE": "15002185856",
           "ISMAINIBS": "1"
       }
        ]
    }

    /*
    data 必须是json数组
    */
    var process = function (data) {
        this.data = data;
        this.cells = [];

        this.getDataByColumn = function (desc, column) {
            var that = this;
            var data = this.sortBy(column, desc, this.sortStringNum);

            return data;
        }

        this.getCells = function () {
            for (var i in this.data[0]) {
                this.cells.push(i);
            }
        }

        this.renderHead = function (container, data) {
            if (data == undefined || data == null) {
                data = this.data;
            }
            if (this.cells.length == 0) {
                this.getCells();
            }
            var html = "<table class='tab'><tr tag='head'>";
            for (var i = 0; i < this.cells.length; i++) {
                html += "<td sort='true' tag='" + this.cells[i] + "'>" + this.cells[i] + "</td>";
            }
            html += "</tr></table>";
            $(container).append($(html));
        }

        this.renderBody = function (container, data) {
            if (data == undefined || data == null) {
                data = this.data;
            }
            //clear
            $(container).find("table tr[tag='body']").each(function () {
                $(this).remove();
            })
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += "<tr tag='body'>";
                for (var j = 0; j < this.cells.length; j++) {
                    html += "<td>" + data[i][this.cells[j]] + "</td>";
                }
                html += "</tr>";
            }
            $(container).find("table").append($(html));
        }

        this.render = function (container, data) {
            this.renderHead(container, data);
            this.renderBody(container, data);
            this.event(container);
        }

        this.event = function (container) {
            var that = this;
            $(container).find("td[sort=true]").each(function () {
                this.addEventListener("click", function () {
                    var sort = false;
                    if ($(this).attr("sort") == "true") {
                        sort = true;
                        $(this).attr("sort", "false");
                    } else {
                        sort = false;
                        $(this).attr("sort", "true");
                    }

                    var _data = that.getDataByColumn(sort, $(this).attr("tag"));
                    that.renderBody($("#container"), _data);
                }, false);
            })
        }
    }

    process.prototype.sortStringNum = function (x, y) {
        var reg = /^\d+(\.\d+)?$/;
        var result;
        if (reg.test(x) && reg.test(y)) {
            var iNum1 = parseInt(x);
            var iNum2 = parseInt(y);
            if (iNum1 < iNum2) {
                result = -1;
            } else if (iNum1 > iNum2) {
                result = 1;
            } else {
                result = 0;
            }
        } else {
            result = null; //x.localeCompare(y);
        }
        return result;
    }

    process.prototype.sortBy = function (prop, desc, fn) {
        arr = this.data;
        var props = [],
        ret = [],
        i = 0,
        len = arr.length;
        if (typeof prop == 'string') {
            for (; i < len; i++) {
                var oI = arr[i];
                (props[i] = new String(oI && oI[prop] || ''))._obj = oI;
            }
        }
        else {
            throw '参数类型错误';
        }

        if (fn != null && typeof fn == 'function') {
            props.sort(fn);
        } else {
            props.sort();
        }
        for (i = 0; i < len; i++) {
            ret[i] = props[i]._obj;
        }
        if (desc) {
            ret.reverse();
        }
        return ret;
    }

    var p = new process(data());
    var d = p.getDataByColumn(false, "CUSTID");
    p.render($("#container"), d);


    var arr2 = [];
    arr2.push({ date: "2013-01-01", name: "lee", age: 2 });
    arr2.push({ date: "2013-07-02", name: "wang", age: 12 });
    arr2.push({ date: "2013-02-02", name: "huang", age: 30 });
    arr2.push({ date: "2013-06-03", name: "keke1", age: 14 });
    arr2.push({ date: "2013-02-04", name: "keke2", age: 31 });
    arr2.push({ date: "2013-08-03", name: "keke3", age: 56 });
    arr2.push({ date: "2013-04-05", name: "keke4", age: 22 });
    arr2.push({ date: "2013-05-03", name: "keke5", age: 32 });

    Array.prototype.sortByField = function (column,desc) {
        return this.sortBy(this, column, desc, this.sortByFn);
    }
    Array.prototype.sortBy = function (arr,prop, desc, fn) {
        var props = [],
        ret = [],
        i = 0,
        len = arr.length;
        if (typeof prop == 'string') {
            for (; i < len; i++) {
                var oI = arr[i];
                (props[i] = new String(oI && oI[prop] || ''))._obj = oI;
            }
        }
        else {
            throw '参数类型错误';
        }

        if (fn != null && typeof fn == 'function') {
            props.sort(fn);
        } else {
            props.sort();
        }
        for (i = 0; i < len; i++) {
            ret[i] = props[i]._obj;
        }
        if (desc) {
            ret.reverse();
        }
        return ret;
    }
    Array.prototype.sortByFn = function (x,y) {
        var reg = /^\d+$/g;
        var reg2 = /^\d+$/g;
        var result;
        if (reg.test(x) && reg2.test(y)) {
            var iNum1 = parseInt(x);
            var iNum2 = parseInt(y);
            if (iNum1 < iNum2) {
                result = -1;
            } else if (iNum1 > iNum2) {
                result = 1;
            } else {
                result = 0;
            }
        } else {
            result = x.localeCompare(y);
        }
        return result;
    }

    var arr1 = arr2.sortByField("date");
    var arr3 = arr2.sortByField("date", "desc");
</script>
